package com.woniuxy.lrz.carlproject.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.lrz.carlproject.common.util.Results;
import com.woniuxy.lrz.carlproject.controller.form.formgoods.GoodsPropertyAddOrUpdateForm;
import com.woniuxy.lrz.carlproject.controller.form.formgoods.GoodsPropertyFindForm;
import com.woniuxy.lrz.carlproject.model.GoodProperty;
import com.woniuxy.lrz.carlproject.service.GoodPropertyService;
import com.woniuxy.lrz.carlproject.service.dto.dtogoods.GoodsPropertyCategoryDto;
import com.woniuxy.lrz.carlproject.service.dto.dtogoods.GoodsPropertyDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import javafx.scene.input.Mnemonic;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 属性名表 前端控制器
 * </p>
 *
 * @author lrz
 * @since 2022-09-28
 */
@RestController
@Api(tags = "商品属性接口")
@Validated
@RequestMapping("/carlproject/goodProperty")
public class GoodPropertyController {
    @Resource
    private GoodPropertyService goodPropertyService;

    @PostMapping("/addGoodsProperty")
    @ApiOperation("新增/修改商品属性名")
    public Results addGoodsProperty(@RequestBody @Valid GoodsPropertyAddOrUpdateForm goodsPropertyAddOrUpdateForms){
        goodPropertyService.addGoodsProperty(goodsPropertyAddOrUpdateForms);
        return Results.instance().setMessage("操作成功");
    }

    @DeleteMapping("/deleteGoodsProperty")
    @ApiOperation("删除商品属性名")
    @ApiImplicitParam(name = "propertyId",value = "商品属性名Id")
    public Results deleteGoodsProperty(@NotNull(message = "属性Id不能为空") Integer propertyId){
        goodPropertyService.deleteGoodsProperty(propertyId);
        return Results.instance().setMessage("删除成功");
    }

    @GetMapping("/findGoodsProperty")
    @ApiOperation("加载商品属性信息")
    public Results findGoodsProperty(@Valid GoodsPropertyFindForm goodsPropertyFindForm){
        Page<GoodsPropertyDto> goodsProperty = goodPropertyService.findGoodsProperty(goodsPropertyFindForm);
        return Results.instance().setMessage("加载商品属性信息成功").setData(goodsProperty);
    }
    @ApiOperation("加载商品属性下拉列表")
    @ApiImplicitParam(name = "goodsCategoryId",value = "商品分类Id")
    @GetMapping("/findGoodsPropertyCondition")
    @Cacheable(value = "goods",key = "'goodsProperty:'+#goodsCategoryId")
    public Results findGoodsPropertyCondition(@NotNull(message = "商品分类Id不能为空") Integer goodsCategoryId){
        List<GoodProperty> good_category_id = goodPropertyService.list(new QueryWrapper<GoodProperty>().eq("good_category_id", goodsCategoryId));
        List<GoodsPropertyCategoryDto> goodsPropertyCategoryDtos = BeanUtil.copyToList(good_category_id, GoodsPropertyCategoryDto.class);
        return Results.instance().setData(goodsPropertyCategoryDtos).setMessage("加载商品属性下拉列表成功");
    }
}
